<template>
  <div class="page-container">
    <div class="control-container">
      <div class="right-panel">
        <div class="search-panel">
          <el-form :inline="true" label-width="100">
            <el-form-item label="设备品牌">
              <el-input v-model="query.eqmBrand" />
            </el-form-item>
            <el-form-item label="设备型号">
              <el-input v-model="query.eqmModel" />
            </el-form-item>
            <el-button type="primary" @click="handleSelect">查询</el-button>
            <el-button v-if="hasBtn('equipmentType:edit')" type="primary" @click="handleAdd">添加</el-button>
            <el-button v-if="hasBtn('equipmentType:export')" :loading="saveLoading" type="primary" @click="handleExport">导出</el-button>
          </el-form>
        </div>
        <div class="table-panel">
          <el-table
            v-loading="tableLoading"
            class="grid-data-table"
            height="calc(100vh - 300px)"
            :data="tableData"
          >
            <el-table-column align="center" type="index" width="80" />
            <el-table-column prop="name" label="设备类型名称" />
            <el-table-column prop="eqmBrand" label="设备品牌" />
            <el-table-column prop="shortName" label="设备类型简称" />
            <el-table-column prop="manufacturer" label="生产商" />
            <el-table-column prop="eqmModel" label="设备型号" />
            <el-table-column prop="contaminants" label="监测污染物" />
            <el-table-column v-if="hasBtn('equipmentType:edit')" label="操作">
              <template slot-scope="scope">
                <el-button type="primary" size="small" @click="handleUpdate(scope.row)">修改</el-button>
                <el-button type="primary" size="small" @click="handleDel(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <Pagination
              v-show="total>0"
              :total="total"
              :page.sync="query.page"
              :limit.sync="query.limit"
              @pagination="initData"
            />
          </div>
        </div>
      </div>
    </div>
    <AddEquipmentTypeDialog
      :dialog-visible="dialogVisible"
      :dialog-type="dialogType"
      :obj="obj"
      @closeDialog="closeDialog"
      @flushPage="flushPage"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { hasBtnPermission as hasBtn } from '@/utils/buttons'
import AddEquipmentTypeDialog from './components/AddEquipmentTypeDialog'
import { downloadFile } from '@/utils'
import { getPage, download, del } from '@/api/info/equipmentType'
export default {
  name: 'EquipmentType',
  components: {
    Pagination, AddEquipmentTypeDialog
  },
  data() {
    return {
      saveLoading: false,
      query: {
        page: 1,
        limit: 10,
        eqmModel: null,
        eqmBrand: null
      },
      total: 0,
      tableData: [],
      tableLoading: false,
      dialogVisible: false,
      dialogType: 'detail',
      obj: {}
    }
  },
  created() {
    this.initData()
  },
  methods: {
    hasBtn,
    initData() {
      this.handleSelect()
    },
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 查询
    handleSelect() {
      this.tableLoading = true
      const query = JSON.parse(JSON.stringify(this.query))
      getPage(query).then(res => {
        this.tableData = res.data.data.list
        this.total = res.data.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    // 添加
    handleAdd() {
      this.obj = null
      this.dialogVisible = true
      this.dialogType = 'add'
    },
    // 修改
    handleUpdate(row) {
      this.obj = row
      this.dialogType = 'update'
      this.dialogVisible = true
    },
    handleDel(id) {
      del(id).then(result => {
        this.handleSelect()
      }).catch(() => {

      })
    },
    // 导出
    handleExport() {
      this.saveLoading = true
      const query = JSON.parse(JSON.stringify(this.query))
      download(query).then(result => {
        downloadFile(result, '设备类型', 'xlsx')
        this.saveLoading = false
      }).catch(() => {
        this.saveLoading = false
      })
    },
    closeDialog() {
      this.dialogVisible = false
    },
    clearQuery() {
      this.query = {
        page: 1,
        limit: 10,
        eqmModel: null,
        eqmBrand: null
      }
    },
    flushPage() {
      this.clearQuery()
      this.initData()
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
